package com.shunhao.network.exception

/**
 * Created by kGod on 2017/2/28.
 * Email 18252032703@163.com
 * Thank you for watching my code
 * 自定义异常
 *
 * @author kang gui yang
 */
class ApiException(val detailMessage: String?, val statusCode: Int = 0) :
    RuntimeException(detailMessage) {

    constructor(resultCode: Int) : this(getNetworkExceptionMessage(resultCode))


    companion object {
        /**
         * 请求成功结果码
         */
        const val REQUEST_OK = 200

        /**
         * 请求失败结果码
         */
        private const val REQUEST_FAIL = 201

        /**
         * token失效 token过期，可刷新
         */
        const val REQUEST_TOKEN_FAILED = 50401

        const val NO_AUTH = 50401

        const val NO_AUTH_FRAME = 401

        /**
         * 受限制了
         */
        const val NO_LIMIT_ERROR = 50403

        /**
         * 接口错误
         */
        private const val SERVER_INTERNAL_ERROR = 404

        /**
         * 参数不完整
         */
        private const val PARAMETER_ERROR = 301
        private const val METHOD_NOT_ALLOWED = 102
        private const val UID_OR_PWD_ERROR = 104
        private const val REQUEST_TIMEOUT = 106
        private const val CONNECTION_ERROR = 107

        /**
         * 40301 当前账号无权限操作此接口
         */
        private const val VERIFY_EXPIRED = 40301
        private const val NO_DATA = 109

        /**
         * 将结果码转换成对应的文本信息
         */
        private fun getNetworkExceptionMessage(code: Int): String = when (code) {
            REQUEST_OK -> "接口成功，操作成功"
            REQUEST_FAIL -> "接口成功，操作不成功"
            METHOD_NOT_ALLOWED -> "请求方式不允许"
            PARAMETER_ERROR -> "参数不完整"
            UID_OR_PWD_ERROR -> "用户名或密码错误"
            SERVER_INTERNAL_ERROR -> "接口错误"
            REQUEST_TIMEOUT -> "请求超时"
            CONNECTION_ERROR -> "连接错误"
            VERIFY_EXPIRED -> "当前账号无权限操作此接口"
            NO_DATA -> "没有数据"
            NO_AUTH -> "请先登录"
            110 -> "该用户已存在"
            else -> "未知错误"
        }
    }
}